/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exception.DAOCandidatException;
import dao.exception.DAOCandidatureException;
import dao.interfaces.DAOCandidature;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Candidat;
import model.Candidature;
import utils.DateUtils;
import utils.DateUtilsException;

/**
 *
 * @author Paul
 */
public class DAOCandidatureImpl implements DAOCandidature {

    private DAOInit daoConnection;

    public DAOCandidatureImpl(DAOInit daoConnection) {
        this.daoConnection = daoConnection;
    }

    @Override
    public ArrayList<Candidature> findAll() throws DAOCandidatureException {
        String query = "SELECT c.idFormation, c.idCandidat, c.dateCandidature, c.etatCandidature "
                + "FROM candidature c,candidat ct, formation f where "
                + "c.idFormation = f.idFormation and c.idCandidat = ct.idCandidat";
        ArrayList<Candidature> candidatures = new ArrayList<Candidature>();
        try {
            Connection conn = daoConnection.getConnection();//Creation de la connexion
            Statement stmt = conn.createStatement();
            ResultSet res = stmt.executeQuery(query);
            while (res.next()) {
                int idCandidat = res.getInt("idCandidat");
                int idFormation = res.getInt("idFormation");
                Date dateCandidature = res.getDate("dateCandidature");
                String etatCandidature = res.getString("etatCandidature");
                Candidature candidature = new Candidature(idFormation, idCandidat, dateCandidature, etatCandidature);
                candidatures.add(candidature);
            }
            if (candidatures.isEmpty()) {
                throw new DAOCandidatureException("Erreur de récupération des candidatures : veuillez contacter avec l'administrateur du site");
            }
            res.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return candidatures;
    }

    @Override
    public Candidature find(int idCandidat) throws DAOCandidatureException {
        String query = "SELECT c.idFormation, c.idCandidat, c.dateCandidature, c.etatCandidature "
                + "FROM candidature c,candidat ct, formation f where "
                + "c.idFormation = f.idFormation and c.idCandidat = ?";
        Candidature candidature = null;
        try {
            Connection conn = daoConnection.getConnection();//Creation de la connexion
            PreparedStatement stmt = conn.prepareStatement(query);
            stmt.setInt(1, idCandidat);
            ResultSet res = stmt.executeQuery(query);
            while (res.next()) {
                int idFormation = res.getInt("idFormation");
                Date dateCandidature = res.getDate("dateCandidature");
                String etatCandidature = res.getString("etatCandidature");
                candidature = new Candidature(idFormation, idCandidat, dateCandidature, etatCandidature);
            }
            if (candidature == null) {
                throw new DAOCandidatureException("Erreur de récupération des candidatures : veuillez contacter avec l'administrateur du site");
            }
            res.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return candidature;
    }

    @Override
    public void create(int idFormation, int idCandidat, Date dateCandidature, String etatCandidature) throws DAOCandidatureException {
        String query = "INSERT INTO candidature (idFormation, idCandidat, dateCandidature, etatCandidature) VALUES (?, ?, ?, ?) ";
        Connection connection = daoConnection.getConnection();
        try {
            PreparedStatement stmt = connection.prepareStatement(query);
            stmt.setInt(1, idFormation);
            stmt.setInt(2, idCandidat);
            stmt.setDate(3, dateCandidature);
            stmt.setString(4, etatCandidature);
            stmt.executeUpdate();
            stmt.close();
            connection.close();
        } catch (SQLException exc) {
            throw new DAOCandidatureException("L'insertion n'a pu se faire : contacter l'administrateur du site");
        }
    }

    @Override
    public void update(Candidat candidat) throws DAOCandidatureException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void delete(int idCandidat) throws DAOCandidatureException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
